package com.jx.jingxuan.widget.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.jx.jingxuan.util.Tools;

/**
 * <li>Package: {@link SlideLayout}
 * <li>Author: cyh
 * <li>Date: 2018/2/5 11:38
 * <li>Discription: 侧滑布局
 */

public class SlideLayout extends HorizontalScrollView {
    private LinearLayout linearLayout;
    private View rightView, leftView, itemView;

    private int mLeftMenuWidth = 0;
    private int mRightMenuWidth;
    private boolean isOpen;
    private boolean canScroll = true;//是否允许滚动
    private boolean isParentIntercept;//

    private int childsWidth;
    private float mDownX;

    public void setLeftMenuWidth(int leftMenuWidth) {
        mLeftMenuWidth = leftMenuWidth;
    }

    public void setRightMenuWidth(int rightMenuWidth) {
        mRightMenuWidth = rightMenuWidth;
    }

    public SlideLayout(Context context) {
        super(context);
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        setHorizontalScrollBarEnabled(false);

        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        addView(linearLayout);
    }

    public void addChilds(View itemView, final View rightView, final View leftView) {
        if (linearLayout != null) {
            if (leftView != null) {
                this.leftView = leftView;
                linearLayout.addView(leftView);
            }
            if (itemView != null) {
                this.itemView = itemView;
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Tools.getDisplayWidth(getContext().getApplicationContext()), ViewGroup.LayoutParams.WRAP_CONTENT);
                itemView.setLayoutParams(params);
                linearLayout.addView(itemView);
            }
            if (rightView != null) {
                this.rightView = rightView;
                linearLayout.addView(rightView);
            }
        }
    }

    public void addRChilds(View itemView, View rightView) {
        addChilds(itemView, rightView, null);
    }

    public void addLChilds(View itemView, View leftView) {
        addChilds(itemView, null, leftView);
    }

    public void close() {
        isOpen = false;
        this.smoothScrollTo(mLeftMenuWidth, 0);
    }

    public void openLeftMenu() {
        isOpen = true;
        this.smoothScrollTo(0, 0);
        onOpenMenu();
    }

    public void openRightMenu() {
        isOpen = true;
        this.smoothScrollBy(mRightMenuWidth + mLeftMenuWidth + mRightMenuWidth, 0);
        onOpenMenu();
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
        if (canScroll) {
            if (rightView != null && rightView.getParent() == null) {
                linearLayout.addView(rightView);
            }
            if (leftView != null && leftView.getParent() == null) {
                linearLayout.addView(leftView, 0);
            }
        } else {
            if (rightView != null) {
                linearLayout.removeView(rightView);
            }
            if (leftView != null) {
                linearLayout.removeView(leftView);
            }
        }
    }

    public WbaseRecycleAdapter2 getAdapter() {
        View view = this;
        while (true) {
            view = (View) view.getParent();
            if (view instanceof RecyclerView) {
                break;
            }
        }
        return (WbaseRecycleAdapter2) ((RecyclerView) view).getAdapter();
    }

    private void onOpenMenu() {
        getAdapter().holdOpenItem(this);
    }

    public void closeOpenMenu() {
        if (!isOpen()) {
            getAdapter().closeOpenItem();
        }
    }

    public SlideLayout getScrollingItem() {
        return getAdapter().getScrollingItem();
    }

    public void setScrollingItem(SlideLayout scrollingItem) {
        getAdapter().setScrollingItem(scrollingItem);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        scrollTo(mLeftMenuWidth, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (rightView != null) {
            mRightMenuWidth = rightView.getMeasuredWidth();
        }
        if (leftView != null) {
            mLeftMenuWidth = leftView.getMeasuredWidth();
        }

        if (itemView != null) {
            int heightSpec = MeasureSpec.makeMeasureSpec(itemView.getMeasuredHeight(), MeasureSpec.EXACTLY);
            int widthSpec;
            if (rightView != null) {
                widthSpec = MeasureSpec.makeMeasureSpec(mRightMenuWidth, MeasureSpec.EXACTLY);
                rightView.measure(widthSpec, heightSpec);
            }

            if (leftView != null) {
                widthSpec = MeasureSpec.makeMeasureSpec(mLeftMenuWidth, MeasureSpec.EXACTLY);
                leftView.measure(widthSpec, heightSpec);
            }
        }

        computeChildsWidth();
    }

    private boolean clickInArea(MotionEvent ev, View view) {
        if (view != null) {
            int[] location = new int[2];
            view.getLocationInWindow(location);
            if (ev.getX() >= location[0] && ev.getX() <= location[0] + view.getMeasuredWidth()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                closeOpenMenu();
                mDownX = ev.getX();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (isOpen() && !clickInArea(ev, rightView) && !clickInArea(ev, leftView)) {
                    setScrollingItem(null);
                    getAdapter().closeOpenItem();
                    return false;
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (getScrollingItem() != null && getScrollingItem() != this) {
            return false;
        }
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downTime = System.currentTimeMillis();
                closeOpenMenu();
                setScrollingItem(this);
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollx = getScrollX();
                if ((scrollx == 0 && mDownX - ev.getX() <= -10)
                        || (scrollx >= childsWidth - getMeasuredWidth() && mDownX
                        - ev.getX() >= 10)) {
                    isParentIntercept = true;
                } else {
                    isParentIntercept = false;
                }
                if (isParentIntercept || !canScroll) {
                    //父view进行拦截，做父view自己的操作
                    getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                } else {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    return super.onTouchEvent(ev);
                }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                setScrollingItem(null);
                int scrollX = getScrollX();
                if (System.currentTimeMillis() - downTime <= 200 && scrollX == mLeftMenuWidth) {
                    return false;
                }
                if (scrollX < mLeftMenuWidth / 2) {
                    openLeftMenu();
                }
                if (scrollX >= mLeftMenuWidth / 2 && scrollX <= mLeftMenuWidth + mRightMenuWidth / 2) {
                    close();
                }
                if (scrollX > mLeftMenuWidth + mRightMenuWidth / 2) {
                    openRightMenu();
                }
                return false;
        }
        return super.onTouchEvent(ev);
    }

    long downTime = 0;

    private void computeChildsWidth() {
        childsWidth = 0;
        for (int i = 0; i < getChildCount(); i++) {
            childsWidth += getChildAt(i).getMeasuredWidth();
        }
    }


}
